package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.base.exception.BusinessException;
import com.weixing.mall.core.utils.RequestUtil;
import com.weixing.mall.provider.api.model.vo.ActivityProductVo;
import com.weixing.mall.provider.mapper.GoodsConcernMapper;
import com.weixing.mall.provider.mapper.ProductMapper;
import com.weixing.mall.provider.model.domain.GoodsConcern;
import com.weixing.mall.provider.model.dto.GoodsConcernDto;
import com.weixing.mall.provider.model.vo.GoodsConcernVo;
import com.weixing.mall.provider.service.IGoodsConcernService;
import com.weixing.mall.util.ListUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * <p>
 * 商品模块-加入收藏 实现类
 * </p>
 * @author lgc
 * @date 2019/12/6 15:27
 * @Version 1.0
 */
@Service
public class GoodsConcernServiceImpl extends ServiceImpl<GoodsConcernMapper,GoodsConcern> implements IGoodsConcernService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public void save(GoodsConcernDto goodsConcernDto,Long mid) {
        int flag = baseMapper.selectMyStoreByGoodsIdAndProductId(goodsConcernDto.getGoodsId(),goodsConcernDto.getProductId(),mid);
        if(flag == 0 && goodsConcernDto != null&& goodsConcernDto.getGoodsId() != null&& goodsConcernDto.getProductId() != null&&mid != null){
            GoodsConcern goodsConcern = new GoodsConcern();
            BeanUtils.copyProperties(goodsConcernDto,goodsConcern);
            goodsConcern.setMemberId(mid);
            save(goodsConcern);
        }
    }

    @Override
    public void delBatch(int[] goodsId) {
        baseMapper.delByGoodIds(goodsId);
    }

    @Override
    public void del(Long id) {
        baseMapper.delByGoodId(id);
    }

    @Override
    public IPage<GoodsConcernVo>  selectByMemberId(Page page,Long member_id) {
        return baseMapper.selectByMemberIdByPage(page,member_id);
    }

    @Override
    public List<ActivityProductVo> selectGoodsConcern(long[] product_Ids) {

        List<ActivityProductVo> listAProV =  productMapper.findActProductVoByIds(product_Ids);

        return listAProV;
    }

    @Override
    public int selectCount(Long mid) {
        return baseMapper.selectCount(mid);
    }

    /*
    编辑购车时---批量选择加入收藏夹
     */
    @Override
    public void saveBatch(List<Long> productIds,Long mid) {
        List<GoodsConcernVo> goodsConcernVos = baseMapper.selectByMemberId(mid);
        Iterator<Long> iterator = productIds.iterator();
        while (iterator.hasNext()){
            Long next = iterator.next();
            for (GoodsConcernVo goodsConcernVo : goodsConcernVos) {
                if(goodsConcernVo.getProductId().longValue()==next){
                    iterator.remove();
                }
            }
        }
        List<GoodsConcern> concernList = new ArrayList<>();
        if(productIds.size() > 0){
            List<GoodsConcernVo> list = productMapper.selectByIds(productIds);
            for (GoodsConcernVo goodsConcernVo : list) {
                goodsConcernVo.setMemberId(mid);
                GoodsConcern goodsConcern = new GoodsConcern();
                BeanUtils.copyProperties(goodsConcernVo,goodsConcern);
                concernList.add(goodsConcern);
                //save(goodsConcern);
            }
        }
        if(concernList.size() > 0){
            saveBatch(concernList, ListUtil.SIZE);
        }
    }

    @Override
    public int selectMyStore(Long memberId) {
        return baseMapper.selectMyStore(memberId);
    }

    @Override
    public void cancel(Long mid, Long goodsId, Long productId) {
       LambdaUpdateWrapper<GoodsConcern> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
       lambdaUpdateWrapper.eq(GoodsConcern::getGoodsId,goodsId).eq(GoodsConcern::getProductId,productId).eq(GoodsConcern::getMemberId,mid);
       remove(lambdaUpdateWrapper);
    }
}
